package com.dragon.client.controller.supplies;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.supplies.IProcurementService;
import com.dragon.repository.domain.dto.ProcurementDTO;
import com.dragon.repository.domain.vo.ProcurementVO;
import com.dragon.repository.entity.Procurement;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 采购单 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-11-15
 */
@RestController
@RequestMapping("procurement")
@Api(tags = "采购")
public class ProcurementController {

    @Autowired
    IProcurementService service;

    @GetMapping("list")
    @ApiOperation(value = "采购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "id", value = "详情id", required = false, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数（标题）", required = false, type = "string"),
            @ApiImplicitParam(name = "state", value = "审核状态", required = false, type = "int")
    })
    public ResponseData<List<ProcurementDTO>> listProcurement(@RequestParam Integer page,
                                                              @RequestParam Integer limit,
                                                              @RequestParam(required = false) Integer id,
                                                              @RequestParam(required = false) String search,
                                                              @RequestParam(required = false) Integer state){
        Page<Procurement> procurementPage = service.listProcurement(page,limit,id,search, state,1);
        return ResponseData.success(ProcurementDTO.convertList(procurementPage.getRecords()), procurementPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    @AddPage
    public ResponseData addProcurement(@RequestBody @Validated(Insert.class) ProcurementVO vo){
        return ResponseData.success(service.addProcurement(ProcurementVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updateProcurement(@RequestBody @Validated(Update.class)ProcurementVO vo){
        service.updateProcurement(ProcurementVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("receiptProduct")
    @ApiOperation(value = "收获操作")
    public ResponseData receiptProduct(@RequestBody Procurement procurement){
        service.receiptProduct(procurement);
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData deleteProcurement(@ApiParam(name = "id", value = "主表id", required = true, type = "int")
                                          @RequestParam Integer id){
        service.deleteById(id);
        return ResponseData.success();
    }

}
